package com.sam.clone;

public class CloneTest {

	/**
	 * clone是浅拷贝 深拷贝需自己实现
	 * 
	 * @param args
	 * @throws CloneNotSupportedException
	 */
	public static void main(String[] args) throws CloneNotSupportedException {

		Persion p1 = new Persion("sam", 24, new Persion("qmx", 25, null));
		System.out.println(p1);

		Persion p2 = (Persion) p1.clone();

		System.out.println(p2);

		System.out.println(p1.equals(p2));
		System.out.println(p1 == p2);

		p1.getPersion().setName("qmxiszhu");
		p1.setName("haha");
		System.out.println("p1:" + p1);
		System.out.println("p2:" + p2);

	}

	public static class Persion implements Cloneable {

		private String name;

		private Integer age;

		private Persion persion;

		public Persion getPersion() {
			return persion;
		}

		public void setPersion(Persion persion) {
			this.persion = persion;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Integer getAge() {
			return age;
		}

		public void setAge(Integer age) {
			this.age = age;
		}

		public Persion(String name, Integer age, Persion persion) {
			this.name = name;
			this.age = age;
			this.persion = persion;
		}

		public Persion() {

		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((age == null) ? 0 : age.hashCode());
			result = prime * result + ((name == null) ? 0 : name.hashCode());
			result = prime * result
					+ ((persion == null) ? 0 : persion.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Persion other = (Persion) obj;
			if (age == null) {
				if (other.age != null)
					return false;
			} else if (!age.equals(other.age))
				return false;
			if (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equals(other.name))
				return false;
			if (persion == null) {
				if (other.persion != null)
					return false;
			} else if (!persion.equals(other.persion))
				return false;
			return true;
		}

		@Override
		public String toString() {
			return "Persion [name=" + name + ", age=" + age + ", persion="
					+ persion + "]";
		}

		@Override
		protected Object clone() throws CloneNotSupportedException {
			return super.clone();
		}

	}

}
